<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Login.php 130 2009-05-21 14:23:24Z movoin $
 */

/**
 * Controller_Login 登录控制台
 *
 * @package Site
 * @subpackage Admin
 *
 */

//{{include
FLEA::loadClass('Controller_Base');
//}}

class Controller_Login extends Controller_Base
{

    var $tblUser;

    /**
     * 构造函数
     *
     * @return Controller_Login
     */
    function Controller_Login()
    {
        // 构造父类
        parent::Controller_Base();
        $this->tblUser =& FLEA::getSingleton( 'Table_User' );
    }

    /**
     * 显示登录界面或跳转至控制台
     *
     */
    function actionIndex()
    {
        $admin = $this->_dispatcher->getUser();
        if($admin)
            redirect($this->_url('Default'));
        $this->_executeView('login');
    }

    /**
     * 登录
     *
     */
    function actionCheck()
    {
        $username = trim($_POST['username']);
        $password = md5($_POST['password']);

        $userinfo = $this->tblUser->find(array(array('username',$username),array('password',$password)));

        if($userinfo){
            $user = array(
                'user_id' => $userinfo['user_id'],
                'username' => $username,
            );
            $this->_dispatcher->setUser($user, 'ADMIN');
            redirect($this->_url('Default'));
        }else{
            js_alert('账户或密码错误,请重新登录!', 'history.back(-1)');
        }
    }

    /**
     * 登出
     *
     */
    function actionLogout()
    {
        $this->_dispatcher->clearUser();
        redirect($this->_url('Login'));
    }

}
